/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.atomojo.app;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.routing.Filter;

/**
 *
 * @author alex
 */
public class HostLogFilter extends Filter {

   Logger log;
   
   public HostLogFilter(Logger log) {
      this.log = log;
      log.setLevel(Level.FINE);
   }
   
   @Override
   protected int beforeHandle(Request request, Response response) {
      request.getAttributes().put("org.restlet.startTime",System.currentTimeMillis());

      return CONTINUE;
   }
   
   
   @Override
   protected void afterHandle(Request request, Response response) {
      long startTime = (Long) request.getAttributes().get(
                "org.restlet.startTime");
      int duration = (int) (System.currentTimeMillis() - startTime);

      String domain = request.getHostRef().getHostDomain();
      
      log.fine(formatDefault(request, response,duration));
   }
   
   
protected String formatDefault(Request request, Response response,
            int duration) {
        StringBuilder sb = new StringBuilder();
        long currentTime = System.currentTimeMillis();

        // Append the date of the request
        sb.append(String.format("%tF", currentTime));
        sb.append('\t');

        // Append the time of the request
        sb.append(String.format("%tT", currentTime));
        sb.append('\t');

        // Append the client IP address
        String clientAddress = request.getClientInfo().getAddress();
        sb.append((clientAddress == null) ? "-" : clientAddress);
        sb.append('\t');

        sb.append('-');
        sb.append('\t');

        // Append the server IP address
        String serverAddress = response.getServerInfo().getAddress();
        sb.append((serverAddress == null) ? "-" : serverAddress);
        sb.append('\t');

        // Append the server port
        Integer serverport = response.getServerInfo().getPort();
        sb.append((serverport == null) ? "-" : serverport.toString());
        sb.append('\t');

        // Append the method name
        String methodName = request.getMethod().getName();
        sb.append((methodName == null) ? "-" : methodName);

        // Append the resource path
        sb.append('\t');
        String resourcePath = request.getResourceRef().getPath();
        sb.append((resourcePath == null) ? "-" : resourcePath);

        // Append the resource query
        sb.append('\t');
        String resourceQuery = request.getResourceRef().getQuery();
        sb.append((resourceQuery == null) ? "-" : resourceQuery);
        // Append the status code
        sb.append('\t');
        sb.append((response.getStatus() == null) ? "-" : Integer
                .toString(response.getStatus().getCode()));

        // Append the returned size
        sb.append('\t');
        if (response.getEntity() == null) {
            sb.append('0');
        } else {
            sb.append((response.getEntity().getSize() == -1) ? "-" : Long
                    .toString(response.getEntity().getSize()));
        }

        // Append the received size
        sb.append('\t');
        if (request.getEntity() == null) {
            sb.append('0');
        } else {
            sb.append((request.getEntity().getSize() == -1) ? "-" : Long
                    .toString(request.getEntity().getSize()));
        }

        // Append the duration
        sb.append('\t');
        sb.append(duration);

        // Append the host reference
        sb.append('\t');
        sb.append((request.getHostRef() == null) ? "-" : request.getHostRef()
                .toString());

        // Append the agent name
        sb.append('\t');
        String agentName = request.getClientInfo().getAgent();
        sb.append((agentName == null) ? "-" : agentName);

        // Append the referrer
        sb.append('\t');
        sb.append((request.getReferrerRef() == null) ? "-" : request
                .getReferrerRef().getIdentifier());

        return sb.toString();
    }
}
